Skip to content

Snap the sulfate ratio XT to 2 or 1 when it is within roundoff of it - #3

Open
mwest1066 wants to merge 1 commit into
partmc-libraryfrom
xt-roundoff-tolerance
Open

mwest1066 wants to merge 1 commit into
partmc-libraryfrom
xt-roundoff-tolerance

Conversation

@mwest1066

@mwest1066 mwest1066 commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Snap the sulfate ratio XT to 2 or 1 when it is within roundoff of it

Branch xt-roundoff-tolerance (one commit on partmc-library at 2c5e006, i.e. after PR #2).
Files: MOSAIC_v2/aerosol/module_mosaic_ext.f90 (+32: a new subroutine
snap_XT and one call after each of the four computations of XT),
MOSAIC_v2/datamodules/module_data_mosaic_aero.f90 (+13: the parameter
xt_snap_rtol = 1.0e-9_r8 and its comment).

Problem

MOSAIC chooses between its sulfate-poor and sulfate-rich treatments with
the sulfate ratio XT = (NH4 + Na + 2 Ca) / (SO4 + MSA/2):

routine test
MESA (MDRH from jsulf_poor or jsulf_rich) XT < 1 .and. XT > 0 (excess sulfate, always liquid), XT >= 2
ASTEM_update_phase_eqblm (the same decision) XT < 1 .and. XT > 0, XT >= 2
compute_activities (fully dissociated vs. HSO4- equilibrium) XT >= 2
ions_to_electrolytes XT >= 2
MESA_estimate_eleliquid XT >= 2; XT_d >= XNa_d, XT_d > 1
form_electrolytes, conform_electrolytes XT >= 2
ASTEM_flux_wet_case* users of the XT returned by ions_to_electrolytes (module_mosaic_astem.f90 ~1266, ~2858) and ASTEM_flux_dry (~2094) XT < 2 .and. XT >= 0

XT is computed in four places with the same expression: calculate_XT,
and inline in ions_to_electrolytes, form_electrolytes and
conform_electrolytes (each has the call to calculate_XT commented out
with ! EFFI).

A particle that is exactly neutral, e.g. NH4 = 2 SO4 as
electrolytes_to_ions makes it from (NH4)2SO4, does not stay exactly at
XT = 2 in PartMC: PartMC stores volumes and converts them to moles at every
step, and after that round trip XT is a few units of roundoff above or
below 2. Such a particle then takes the sulfate-poor or the sulfate-rich
branch depending on the last bits, which change from step to step, so it is
misclassified at random. In the urban plume scenario this is a large share
of the particles: in the 25 hourly outputs of the run below (10000
particles), 99327 of 381844 particle-outputs (26%) have an XT within a
relative 1e-12 of 2, and the count is the same within 1e-9 and 1e-6:
nothing lies between roundoff and 1e-6. (XT computed from PartMC's output
masses; with flat_source weighting, 56128 of 283055, 20%.) None is near 1
in this scenario.

History

The 2014 changes show that the boundary and the consistency between
routines were deliberate: ions_to_electrolytes has
XT.ge.1.9999 commented out by "RAZ 4/16/2014" and replaced by
XT.ge.2.0 ("Slightly different logic, consistent with that in
compute_activities subr."); compute_activities "changed .gt. to .ge.
RAZ 4/16/2014"; form_electrolytes and conform_electrolytes replaced
XT .ge. 1.9999 by XT .ge. 2.0 on "RAZ 11/10/2014", and the ASTEM tests
XT.lt.2.0 carry the same date. So a neutral particle is meant to be
sulfate-poor, in every routine. With roundoff that holds only for the
particles that land at or above 2.

Measurements

PartMC (update_mosaic, 1716875) linked against MOSAIC built as
compile.sh does for gfortran (-O2 -g -fallow-argument-mismatch,
gfortran 14.3, Perlmutter CPU node): this branch against partmc-library
as it is (2c5e006). Urban plume scenario, 24 h, 10000 particles, del_t 60
s, hourly output, rand_init 7, coagulation off, flat weighting. With
coagulation off and flat weighting no random decision of PartMC depends on
the particles' composition, so every run has the same particles (checked:
the particle ids agree in every output) and the differences between runs
come from the chemistry alone. Numbers: largest relative difference over
the 24 hourly outputs.

Effect of the change (partmc-library against this branch):

quantity largest rel. difference
gas NH3 8.9% this branch lower, in the evening (hours 18 to 23)
gas HNO3, HCl 0.67%, 0.40%
bulk NO3 2.4% this branch higher early (hours 1 to 2), lower in the evening (-1.8% at hour 23)
bulk Cl 2.6% the same pattern as NO3
bulk NH4 0.50%
bulk H2O 1.6% this branch lower in the evening
bulk SO4, SOA 2e-5, 6e-4
per particle, |dm| / dry mass median 5.9e-4, 99th percentile 0.27
particles on the other hysteresis leg 710 of 362864 particle-outputs (0.2%)

The same with flat_source weighting: gas NH3 8.9%, bulk NO3 1.0%, Cl
1.2%, H2O 1.0%. The current code treats a random part of the neutral
particles as sulfate-rich (more acidic: less nitrate and chloride taken up
early, a different MDRH and water); this branch treats all of them as
neutral.

Run-to-run "order noise": PartMC hands its particles to MOSAIC as bins
1..n. A run in which the particles are handed over in another order, a new
random one at every step (a test-only change of PartMC's
mosaic_from_partmc/mosaic_to_partmc, below), should differ only by
roundoff:

gas NH3 bulk NO3 bulk NH4 bulk Cl bulk H2O per particle p99 other leg
partmc-library, shuffled vs. not (seed 1; seed 2) 3.2e-3; 4.8e-3 8e-3; 1e-2 3e-3; 1e-3 5e-3; 8e-3 3e-3; 2e-3 2.7e-2; 2.5e-2 554; 586
this branch, shuffled vs. not (seed 1; seed 2) 9.9e-7; 9.6e-7 1e-7; 3e-7 9e-8; 2e-7 4e-7; 9e-7 2e-7; 7e-7 8.2e-6; 8.1e-6 69; 67

So nearly all of the order noise of the current code (1e-3 to 1e-2 in
bulk species, a few percent of a particle's dry mass) is this switch; with
neutral particles treated as neutral it falls to the 1e-7 level.

Sensitivity to the tolerance: the same run with xt_snap_rtol = 1e-5
instead of 1e-9 differs from it by at most 4e-7 in bulk NO3, 1.2e-6 in gas
NH3 and 4.5e-6 per particle (p99), i.e. by the order noise: the result
does not depend on the value between these bounds.

Cost: none measurable (wall time of the 24 h run 334 s with this branch,
332 s without; runs side by side on one node).

Test-only PartMC change used for the shuffled runs (src/mosaic.F90)

A module array mosaic_bin_of_part, the identity unless the environment
variable PMC_MOSAIC_SHUFFLE is set to a seed; then a new random
permutation (Fisher-Yates, MINSTD generator) at every call of
mosaic_from_partmc. Both mosaic_from_partmc and mosaic_to_partmc use
offset = ngas_max + (mosaic_bin_of_part(i_part) - 1)*naer_tot instead of
(i_part - 1)*naer_tot. Not proposed for PartMC.

The change

snap_XT(XT) sets XT to exactly 2 when |XT - 2| <= 2 * xt_snap_rtol, and
to exactly 1 when |XT - 1| <= xt_snap_rtol; otherwise it leaves XT as it
is (the sentinel -1 for no sulfate is untouched). It is called right after
each of the four computations of XT, so every routine that tests XT sees
the same value for the same particle and makes the same decision: the
regime the particle is in exactly at the boundary (sulfate-poor at 2, not
"excess sulfate" at 1). xt_snap_rtol = 0 gives back the current
behaviour.

Why 1e-9: the roundoff of XT after a volume-mole round trip is a few units
of 1.1e-16; 1e-9 is about 1e7 times that, so every roundoff case is caught,
also after longer chains of operations. It is far below any physically
meaningful excess of cations or sulfate (a relative excess of 1e-9 of NH4
over 2 SO4 changes nothing measurable), and far below MOSAIC's own
tolerances (rtol_mesa 0.01, ptol_mol_astem 0.01%). The census shows no
particle between 1e-12 and 1e-6 of the boundary, and 1e-5 gives the same
results (above). It is not large enough for single-precision storage of
the amounts (relative roundoff 6e-8); a model that stores floats would
need about 1e-6.

What is not changed, and why:

  • In its sulfate-rich branch ions_to_electrolytes recomputes XT as
    (remaining NH4 + Na) / SO4 and tests <= 1.0 and <= 1.5. These
    thresholds only split the sulfate among H2SO4, bisulfate, letovicite and
    sulfate, and the split is continuous across them (at 1.0 both sides give
    all bisulfate, at 1.5 all letovicite).
  • The sulfate-rich branch of form_electrolytes and
    conform_electrolytes tests XT_prime >= XNa_prime, XNH4_prime >= 1.5
    and XT_prime >= 1. These too only choose the order in which the
    form_* routines are called, and the electrolytes are continuous across
    the thresholds.
  • MESA_estimate_eleliquid tests XNH4_d against 5/3, 1.5 and 1.0 and
    XT_d (= the snapped XT) against XNa_d and 1. These choose the
    starting estimate of the liquid electrolytes for MESA, not the result;
    they are discontinuous, but only in the starting point of an iteration.
    We did not snap them; see "left to judge".

What was checked

  • Builds with gfortran 14.3 (Perlmutter) and PartMC runs the urban plume
    for 24 h with it, with flat and with flat_source weighting (runs
    above).
  • The branch merges cleanly with crustal-threshold-particle-resolved
    (companion PR); the merge was run as well (see there).

Left for the reviewer to judge

  • Whether the tolerance should be a parameter (as now) or a run-time
    variable that a host model can set.
  • Whether to snap at 1.5 as well. XT itself is never tested against 1.5;
    only the recomputed sulfate-rich ratio is, where the split is
    continuous.
  • Whether the thresholds of MESA_estimate_eleliquid (XNH4_d) should be
    snapped too. They only set the starting point of MESA.
  • The changes of the results above are a change of the model's answer for
    neutral particles; we read them as removing a random misclassification,
    in line with the 2014 intent.

Relation to other PRs

  • Based on partmc-library after PR Compile with -O2 and avoid per-bin work-array allocation #2 (-O2 under gfortran, per-bin work
    arrays; merged), which changed no result.
  • Companion PR crustal-threshold-particle-resolved (no crustal-fraction
    limit on the upper hysteresis curve in the particle-resolved framework):
    independent; the two touch different lines.

🤖 Generated with Claude Code

MOSAIC chooses between its sulfate-poor and sulfate-rich treatments with
XT >= 2 (MESA, compute_activities, ions_to_electrolytes,
MESA_estimate_eleliquid, form_electrolytes, conform_electrolytes, and the
ASTEM routines that use the XT these return), and treats 0 < XT < 1 as
excess sulfate (MESA, ASTEM_update_phase_eqblm). An exactly neutral
particle (NH4 = 2 SO4) whose amounts have been through a mass <-> mole
conversion, as in PartMC at every step, has XT a few units of roundoff
above or below 2, so it takes one branch or the other at random.

XT is computed from the ions in four places (calculate_XT, and inline in
ions_to_electrolytes, form_electrolytes and conform_electrolytes, all with
the same expression). Each now calls the new snap_XT, which sets XT to
exactly 2 (or 1) when it is within a relative xt_snap_rtol = 1e-9 of it,
so that every routine puts such a particle in the same regime, the one it
is in exactly at the boundary (in line with the 2014 change from
XT >= 1.9999 to XT >= 2.0 for consistency between routines).

Not changed: the ratio that ions_to_electrolytes recomputes in its
sulfate-rich branch (thresholds 1.0 and 1.5) and the ratios of the
sulfate-rich branch of form_electrolytes and conform_electrolytes
(XT_prime, XNa_prime, XNH4_prime) only split the sulfate among the
electrolytes, and the split is continuous across those thresholds; those
of MESA_estimate_eleliquid (XNa_d, XNH4_d) only set the starting estimate
of MESA.

xt_snap_rtol = 0 gives back the previous behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant